package com.nick.controller;

import com.alibaba.fastjson.JSON;
import com.nick.pojo.Items;
import com.nick.service.ItemService;
import com.nick.service.ipml.ItemServiceIpml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by 下 on 2017/6/13.
 */
@Controller
public class ItemsController{
    //service
    @Autowired
    private ItemService itemService;
    @Autowired
    private ItemServiceIpml itemServiceIpml;


//    根据id查看
@RequestMapping(value = "/{id}",method = RequestMethod.GET)
public String items(@PathVariable int id,Model model){
        List<Items> items=itemService.getItemById(id);
        model.addAttribute("items",items);
        System.out.println("==============="+items.toString());
    return "shop/itemdetails";
}

//    根据ID查询商品
    @RequestMapping(value = "/show",method = RequestMethod.GET)
    public String items(HttpServletRequest request,Model model){
        if (request.getParameter("id")!=null){
            int itemid= Integer.parseInt(request.getParameter("id"));
            List<Items> items=itemService.getItemById(itemid);
            model.addAttribute("items",items);
            System.out.println("==============="+items.toString());
        }
        return "shop/itemdetails";
    }

//    查询所有商品
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String itemList(Model model,HttpServletRequest request){
//        List<Items> items=itemService.findAllItems();
//        分页
        this.itemServiceIpml.showItemsByPage(request,model);
//        model.addAttribute("items",items);
        return "shop/itemlist";
    }

//    添加商品
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(Model model){
        model.addAttribute(new Items());
        return "shop/additem";
    }

    @RequestMapping(value = "/add" ,method = RequestMethod.POST)
    public String add(@Validated Items items){
        itemService.addItem(items);
        return "redirect:/list";
    }

//    更改商品
    @RequestMapping(value = "/{id}/update",method = RequestMethod.GET)
    public String update(@PathVariable int id,Model model){
        model.addAttribute(new Items());
        return "shop/updateitem";
    }
    @RequestMapping(value = "/{id}/update",method = RequestMethod.POST)
    public String update(@PathVariable int id,@Validated Items items){
        itemService.updateItem(items);
        return "redirect:/list";
    }

//    删除商品
    @RequestMapping(value = "/{id}/delete",method = RequestMethod.GET)
    public String delete(@PathVariable int id){
        itemService.deleteItem(id);
        return "redirect:/list";
    }

//分页测试
    @RequestMapping(value = "/myProductPage",method = RequestMethod.GET)
    public String showitemsList(Model model,HttpServletRequest request){
        this.itemServiceIpml.showItemsByPage(request,model);
        return "shop/listtest";
    }

//    模糊查询
    @RequestMapping(value = "/search" ,method = RequestMethod.GET)
    public String findByName(HttpServletRequest request,Model model){
        String name=request.getParameter("search");
        System.out.println(name);
        List<Items> items=itemService.findByName(name);
        model.addAttribute("items",items);
        System.out.println(JSON.toJSONString(items));
        return "shop/itemdetails";
    }
}
